package com.umgsai.thread.thread52;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class MyService1 {
	private ReentrantLock lock = new ReentrantLock();
	private Condition condition = lock.newCondition();
	public void waitMethod() {
		try {
			lock.lock();
			condition.await();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
	}
	
	public void notifyMethod() {
		try {
			lock.lock();
			System.out.println(lock.getWaitQueueLength(condition) + "个线程在等待condition");//10
			condition.signal();
			System.out.println(lock.getWaitQueueLength(condition) + "个线程在等待condition");//9
			condition.signal();
			System.out.println(lock.getWaitQueueLength(condition) + "个线程在等待condition");//8
			condition.signalAll();
			System.out.println(lock.getWaitQueueLength(condition) + "个线程在等待condition");//7
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
	}
	
	public static void main(String[] args) throws InterruptedException {
		final MyService1 service = new MyService1();
		Runnable runnable = new Runnable() {
			
			@Override
			public void run() {
				service.waitMethod();
			}
		};
		Thread[] threads = new Thread[10];
		for (int i = 0; i < threads.length; i++) {
			threads[i] = new Thread(runnable);
		}
		for (int i = 0; i < threads.length; i++) {
			threads[i].start();
		}
		Thread.sleep(2000);
		service.notifyMethod();
	}
}
